<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class UserCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'state' => 'success',
            'data' => $this->collection->transform(function ($val){
                return [
                    'id' => $val->id,
                    'username' => $val->username,
                    'nickname' => $val->nickname,
                    'created_at' => $val->created_at->toDateTimeString(),
                    'last_login' => $val->last_login,
                    'is_del' => $val->is_del,
                    'is_del_label' => $val->is_del ? '禁用中': '启用中',
                    'role' => implode(',',$val->getRoleNames()->toArray())
                ];
            })
        ];
    }
}
